<template>
  <a-modal
    :open="open"
    width="1100px"
    title="打印诊疗单"
    centered
    destroy-on-close
    :ok-button-props="{ disabled: checkedData.length === 0 }"
    @cancel="onCancel"
    @ok="onOk"
  >
    <div class="overflow-y-auto min-h-[40vh] max-h-[80vh]">
      <BaseTable
        :data-source="dataSource"
        column-code="000541"
        row-key="id"
        :expanded-row-keys="selectedRowKeys"
        :row-selection="{ selectedRowKeys, checkedData, onChange: onSelectChange }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'yzzt'">
            <a-tag v-if="record.yzzt === 0" color="blue">新开立</a-tag>
            <a-tag v-if="record.yzzt === 1" color="green">已签署</a-tag>
            <a-tag v-if="record.yzzt === 2" color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'sfxm'">
            <MedicineInfo :value="{ mc: record.sfxm?.mc, gg: record.sfxm?.gg, cj: record.sfxm?.cj }" />
          </template>
          <template v-if="column.dataIndex === 'yf'">
            {{ record.yf?.fyff }}
          </template>
          <template v-if="column.dataIndex === 'jlsl'"> {{ record.jlsl || 0 }} {{ record.sfxm?.jldw }} </template>
          <template v-if="column.dataIndex === 'cfts'"> {{ record.cfts }}天 </template>
          <template v-if="column.dataIndex === 'sl'"> {{ record.sl }}{{ record.sfxm?.dw }} </template>
          <template v-if="column.dataIndex === 'pl'">
            {{ record.pl?.pl }}
          </template>
        </template>
      </BaseTable>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import useDoctorStore from '../../store'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  dataSource: {
    type: Array,
    default: () => []
  },
  printType: {
    type: String,
    default: () => '0'
  }
})

const params = ref({})
const onCancel = () => {
  emit('update:open', false)
  selectedRowKeys.value = []
  checkedData.value = []
}
const selectedRowKeys = ref([])
const checkedData = ref([])
const doctorStore = useDoctorStore()

const onSelectChange = (keys, rows) => {
  selectedRowKeys.value = keys
  checkedData.value = rows
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      checkedData.value = props.dataSource
      selectedRowKeys.value = props.dataSource.map((i) => i.id)
    }
  }
)

const onOk = () => {
  const groupedData = Object.values(
    checkedData.value.reduce((acc, item) => {
      if (!acc[item.zxks]) {
        acc[item.zxks] = []
      }
      acc[item.zxks].push(item)
      return acc
    }, {})
  )
  groupedData.forEach(async (item, index) => {
    const ids = item.map((items) => {
      return items.id
    })
    if (props.printType === '0') {
      params.value = {
        jzbh: doctorStore.currentPatient.jzbh,
        djlx: '诊疗单申请单',
        ids,
        printStatus: Number(props.printType) ?? undefined
      }
    } else {
      params.value = {
        jzbh: doctorStore.currentPatient.jzbh,
        djlx: '诊疗单申请单',
        ids,
        dybh: item[0]?.dybh,
        jfzt: Number(props.printType) ?? undefined
      }
    }
    const { code, msg, data } = await post('/outp/outPrint/printrptDJ_TY', params.value)
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
    if (index === groupedData.length - 1) onCancel()
  })

  // checkedData.value.forEach(async (i) => {
  //   const { code, msg, data } = await post('/outp/outPrint/printrptDJ_TY', {
  //     jzbh: doctorStore.currentPatient.jzbh,
  //     djlx: '诊疗单申请单',
  //     id: i?.id,
  //     dybh: i?.dybh,
  //     jfzt: Number(props.printType) ?? undefined
  //   })
  //   if (code !== 0) return message.error(msg)
  //   window.openExe?.rpt(data) //调用winform打印
  // })
}
</script>
<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
